<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
	<link rel="stylesheet" href="/style/list.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
				<div class="layui-inline">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" id="userName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
				<div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" onclick="SelectData()">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
				<div class="layui-inline" style="float:right">
					<button class="layui-btn layuiadmin-btn-admin" data-type="add" onclick="WinOpenAdd();">添加</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">

            <table class="layui-hide" id="test"></table>
			
			<script type="text/html" id="barStatus">
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail"  onclick="WinStatus({{d.id}},{{d.state}})">{{d.state==1 ? '启用':'关闭'}}</a>
            </script>
			
			<script type="text/html" id="barLower">
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail"  onclick="WinOpenLower({{d.id}})">下级分类</a>
            </script>
			
			<script type="text/html" id="barPic">
                <img src="{{d.pic}}" width="60" height="20" onclick="WinPic('{{d.pic}}');" />
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"  onclick="WinOpenEdit({{d.id}})">编辑</a>
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" onclick="WinOpenDel({{d.id}})">删除</a>-->
            </script>

        </div>
    </div>
</div>
<script src="/layui/layui.js"></script>
<script src="/layui/jquery.min.js"></script>
<script src="/js/common.js"></script>
<style>
    .layui-body{overflow-y: scroll;}	
	.laytable-cell-1-orderId{width:50px;}
</style>
<script type="text/javascript">
    var tip_index = 0;  
	function show(e)
	{
		tip_index = layer.tips($("#orderGoos"+e).html(), "#order"+e, {tips: [2, '#ddd'],time: 0,area: ['300px', 'auto'],});
	}
</script>
<script>
    var table;
    layui.use('table', function(){
        table = layui.table
            ,form = layui.form;

        var pid = $.getUrlParam("pid");
        if(pid==null)
        {
            pid=0;
        }

        table.render({
            elem: '#test'
            ,url:'/goods?url=Classification%2FList&pid='+pid
			,cellMinWidth:80
            ,cols: [[
                {type:'numbers'}
                ,{type: 'checkbox'}
                ,{field:'id',width:60,title:'ID'}
				,{field:'name',title:'分类名称'}
				,{field:'pic',width:100, title:'分类图片', templet: '#barPic', unresize: true}
				,{field:'state',width:80,title:'状态', templet: '#barStatus', unresize: true}
                ,{field:'sort', title:'排序'}
                ,{field:'lower',title:'下级分类', templet: '#barLower', unresize: true}
				,{field:'category',width:250, title:'编辑', templet: '#barDemo', unresize: true}
            ]]
            ,id: 'testReload'
        });

        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });


    });


    function createTime(v){
        return new Date(parseInt(v)).toLocaleString()
    }
    /**重写toLocaleString方法*/
    Date.prototype.toLocaleString = function() {
        var y = this.getFullYear();
        var m = this.getMonth()+1;
        m = m<10?'0'+m:m;
        var d = this.getDate();
        d = d<10?("0"+d):d;
        var h = this.getHours();
        h = h<10?("0"+h):h;
        var M = this.getMinutes();
        M = M<10?("0"+M):M;
        var S=this.getSeconds();
        S=S<10?("0"+S):S;
        return y+"-"+m+"-"+d+" "+h+":"+M+":"+S;}

	/*添加分类*/
	function WinOpenAdd(){
	    var pid = $.getUrlParam("pid");
	    if(pid==null)
        {
            pid=0;
        }
		window.parent.WinOpenAdd(this,"/category/category/add.html?pid="+pid,"添加分类",400,500,1);
	}	
	
	/*编辑分类*/
	function WinOpenEdit(id){
		window.parent.WinOpenEdit(this,"/category/category/edit.html?id="+id,"修改分类资料",400,500,1);
	}
	
	/*删除分类*/
	function WinOpenDel(categoryId){
		window.parent.WinOpenEdit(this,"/category/category/del.html?categoryId="+categoryId,"删除分类",340,300,1);
	}

    function WinOpenLower(pid)
    {
        window.location="?pid="+pid;
    }
	
	/*修改分类状态*/
	function WinStatus(id,state){
		layer.msg('确定修改吗？', {
			btn: ['确定', '取消']
			,yes: function(index, layero){
                if(state==1)
                {
                    state=0;
                }
                else
                {
                    state=1;
                }
                $.ajax({
                    type: "POST",
                    url: "/goodsjson",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(GetJsonDataS1(id,state)),
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        if (data.code == "000000") {
                            //alert("提交成功!!");
                            location.reload();//刷新数据
                        }
                        else {
                            alert(data.errorDetail);
                        }
                    },
                    error: function (message) {
                        alert("提交数据失败！"+message);
                    }
                });


			}
		});
	}

    function GetJsonDataS1(id,state) {
        var json = {
            "url": "Classification/Set",
            "parm":{"id":id,
                "state":state
            }
        };
        return json;
    }
	
	/*查看分类图片*/
	function WinPic(pic){
		layer.msg("<img src='"+pic+"' width='300' height='300' />");
	}

    /**
     *
     * 添加分类
     *
     */
    function Add(body)
    {
        var state = body.find("#state").val();
        var name = body.find("#name").val();
        var pic = body.find("#pic").val();
        var pid = $.getUrlParam("pid");
        if(pid==null)
        {
            pid=0;
        }
        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(GetJsonData(pid,state,name,pic)),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {
                    //alert("提交成功!!");
                    location.reload();//刷新数据
                }
                else {
                    alert(data.errorDetail);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
        return true;
    }

    function GetJsonData(pid,state,name,pic) {
        var json = {
            "url": "Classification/Add",
            "parm":{"pid":pid,
                "state":state,
                "pic":pic,
                "name":name
            }
        };
        return json;
    }

    function Edit(body)
    {
        var state = body.find("#state").val();
        var name = body.find("#name").val();
        var pic = body.find("#pic").val();
        alert(pic);
        var id = body.find("#id").val();
        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(GetJsonDataS2(id,state,name,pic)),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {
                    //alert("提交成功!!");
                    location.reload();//刷新数据
                }
                else {
                    alert(data.errorDetail);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
        return true;
    }

    function GetJsonDataS2(id,state,name,pic) {
        var json = {
            "url": "Classification/Set",
            "parm":{"id":id,
                "state":state,
                "pic":pic,
                "name":name
            }
        };
        return json;
    }
	
    function SelectData() {

        var pid = $.getUrlParam("pid");
        if(pid==null)
        {
            pid=2;
        }
		alert(url);
        //执行重载
        table.reload('testReload', {
            url:url,
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
	
</script>


</body>
</html>